package com.zengfa.task.client.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.zengfa.task.api.enums.JobType;
import com.zengfa.task.api.model.JobDTO;
import com.zengfa.task.api.model.JobNodeDTO;
import com.zengfa.task.client.config.TaskNodeConfig;
import com.zengfa.task.client.model.TaskDefine;
import com.zengfa.task.client.model.FlowNodeDefine;
import com.zengfa.task.client.service.JobClientService;
import com.zengfa.task.client.service.JobWorkerService;
import com.zengfa.task.client.support.ServerApiSupport;

@Service
public class JobClientServiceImpl implements JobClientService {

	@Autowired
	private ServerApiSupport serverApiSupport;
	@Autowired
	private JobWorkerService jobWorkerService;
	
	@Override
	public JobDTO createFlowTask(String jobGroup, String bizId) {
		TaskDefine flowDefine = TaskNodeConfig.TASKS.get(jobGroup);
		if (flowDefine == null) {
			throw new RuntimeException("流程未定义");
		}
		List<FlowNodeDefine> defineNodeList = flowDefine.getDefineFlowNodes();
		if (CollectionUtils.isEmpty(defineNodeList)) {
			throw new RuntimeException("流程节点未定义");
		}
		JobDTO jobDTO = new JobDTO();
		jobDTO.setJobGroup(jobGroup);
		jobDTO.setBizId(bizId);
		jobDTO.setJobType(JobType.FLOW);
		List<JobNodeDTO> nodeList = new ArrayList<>();
		jobDTO.setNodeList(nodeList);
		for (FlowNodeDefine defineNode : defineNodeList) {
			JobNodeDTO jobNodeDTO = new JobNodeDTO();
			nodeList.add(jobNodeDTO);
			jobNodeDTO.setNodeCode(defineNode.getCode());
			jobNodeDTO.setNodeName(defineNode.getDesc());
			jobNodeDTO.setRunType(defineNode.getRunType());
		}
		return serverApiSupport.createTask(jobDTO);
	}

	@Override
	public void runFlowTask(JobDTO jobDTO) {
		JobType jobType = jobDTO.getJobType();
		if (jobType == JobType.FLOW) {
			jobWorkerService.runFlowTask(jobDTO);
		}
	}

	/*
	@Override
	public void runTask(String jobGroup, String bizId) {
		// TODO Auto-generated method stub

	}
	*/

	@Override
	public JobDTO runAsyncSingleTask(String jobGroup, String bizId, Runnable runnable) {
		JobDTO jobDTO = new JobDTO();
		jobDTO.setJobGroup(jobGroup);
		jobDTO.setBizId(bizId);
		jobDTO.setJobType(JobType.SINGLE);
//		jobDTO.setRunType(1);
		jobDTO = serverApiSupport.createTask(jobDTO);
		if(jobDTO == null) {
			return null;
		}
		jobWorkerService.runAsyncTask(jobDTO, runnable);
		return jobDTO;
	}

	@Override
	public JobDTO runSingleTask(String jobGroup, String bizId, Runnable runnable) {
		JobDTO jobDTO = new JobDTO();
		jobDTO.setJobGroup(jobGroup);
		jobDTO.setBizId(bizId);
		jobDTO.setJobType(JobType.SINGLE);
//		jobDTO.setRunType(4);
		jobDTO = serverApiSupport.createTask(jobDTO);
		if(jobDTO == null) {
			return null;
		}
		jobWorkerService.runTask(jobDTO, runnable);
		return jobDTO;
	}

	@Override
	public void stopTask(String jobGroup, String bizId) {
		serverApiSupport.stopJob(jobGroup, bizId);
	}

	@Override
	public JobDTO getTask(String jobGroup, String bizId) {
		// TODO Auto-generated method stub
		return null;
	}

}
